﻿// See https://aka.ms/new-console-template for more information
using System;

abstract class Shape
{
    public double[] Edges { get; protected set; }

    public abstract double CalculateArea();

    public abstract bool IsValid();
}

class Triangle : Shape
{
    public Triangle(double[] edges)
    {
        Edges = edges;
    }

    public override double CalculateArea()
    {
        if (!IsValid())
            throw new InvalidOperationException("The given edges do not form a valid triangle.");

        double s = (Edges[0] + Edges[1] + Edges[2]) / 2;
        return Math.Sqrt(s * (s - Edges[0]) * (s - Edges[1]) * (s - Edges[2]));
    }

    public override bool IsValid()
    {
        return Edges[0] + Edges[1] > Edges[2] &&
               Edges[0] + Edges[2] > Edges[1] &&
               Edges[1] + Edges[2] > Edges[0];
    }
}

class Rectangle : Shape
{
    public Rectangle(double[] edges)
    {
        Edges = edges;
    }

    public override double CalculateArea()
    {
        if (!IsValid())
            throw new InvalidOperationException("The given edges do not form a valid rectangle.");

        return Edges[0] * Edges[2];
    }

    public override bool IsValid()
    {
        return Edges[0] > 0 && Edges[1] > 0;
    }
}

class Square : Shape
{
    public Square(double[] edges)
    {
        Edges = edges;
    }

    public override double CalculateArea()
    {
        if (!IsValid())
            throw new InvalidOperationException("The given edges do not form a valid square.");

        return Edges[0] * Edges[0];
    }

    public override bool IsValid()
    {
        return Edges[0] > 0;
    }
}

class Program
{
    public static void Main()
    {
        Console.WriteLine("输入边的长度:");
        string input = Console.ReadLine();
        string[] numStr = input.Split(' ');
        double[] edges = new double[numStr.Length];

        for (int i = 0; i < numStr.Length; i++)
        {
            if (double.TryParse(numStr[i], out double parsedNumber))
            {
                edges[i] = parsedNumber;
            }
            else
            {
                Console.WriteLine($"Cannot parse the {i + 1}th number: {numStr[i]}");
            }
        }

        Array.Sort(edges); // 对数组排序

        Shape shape;

        if (edges.Length == 3)
            shape = new Triangle(edges);
        else if (edges.Length == 4)
        {
            if (edges[0] == edges[1] && edges[1] == edges[2] && edges[2] == edges[3])
                shape = new Square(edges);
            else if (edges[0] == edges[1] && edges[2] == edges[3])
                shape = new Rectangle(edges);
            else
            {
                Console.WriteLine("输入的四边形既不是长方形也不是正方形");
                return;
            }
        }
        else
        {
            Console.WriteLine("输入边的数目应为3或4");
            return;
        }

        Console.WriteLine($"Area: {shape.CalculateArea()}");
    }
}
